package com.foreknow.service.impl;

import com.foreknow.dao.inpl.AbcDaoImpl;
import com.foreknow.dao.inpl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Abc;
import com.foreknow.model.Guestbook;
import com.foreknow.service.AbcService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class AbcServiceImpl implements AbcService {
    //1. 链接数据库
    DBManager dbManager= DBManager.getInstance();
    Connection conn =dbManager.getConnection();

   AbcDaoImpl abcDao= new AbcDaoImpl();
    @Override
    public Abc isLogin(int id, String name) {
        //调用BaseDao中的Connection(conn)方法
        abcDao.setConnection(conn);
        Abc abc =abcDao.isLogin(id,name);
        return abc;
    }

    //查询所有
    @Override
    public List<Object> query() {
        //调用BaseDao中的Connection(conn)方法
        abcDao.setConnection(conn);
        List<Object>list=abcDao.query();
        return list;
    }

    //修改
    @Override
    public boolean update(Abc abc) {
        //调用BaseDao中的Connection(conn)方法
        abcDao.setConnection(conn);
        Boolean update=null;
        try {
            update= abcDao.update(abc);
            //手动提交事务提交事务
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return update;
    }

    //增加
    @Override
    public boolean add(Abc abc) {
        //调用BaseDao中的Connection(conn)方法
        abcDao.setConnection(conn);
        boolean isRight = false;
        try {
             isRight=abcDao.add(abc);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return isRight;
    }

    //删除
    @Override
    public boolean delete(int id) {
        //调用BaseDao中的Connection(conn)方法
        abcDao.setConnection(conn);
        boolean delete=false;
        try {
            delete=abcDao.delete(id);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return delete;
    }
}
